package com.wmyg.antifake.controller;

import com.wmyg.antifake.service.PayNoticeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * @author 王卓逸
 * @program TODO
 * @description TODO
 * @create 2019-05-21
 */
@Slf4j
@RestController
@RequestMapping(value = "/notice")
@ApiIgnore
public class PayNoticeController {

    @Autowired
    PayNoticeService payNoticeService;

    @RequestMapping(value = "/wxpay")
    public String wxpayNotice(@RequestBody String notifyData) {
        log.info("[wxpay] receive the notice: {}", notifyData);
        return payNoticeService.wxpayNoticeHandle(notifyData);
    }

    @RequestMapping(value = "/alipay")
    public String alipayNotice(HttpServletRequest request) {
        log.info("[alipay] receive the notice: {}", request.getParameterMap());
        // 参数Map
        Map<String, String[]> properties = request.getParameterMap();
        // 返回值Map
        Map<String, String> returnMap = new HashMap<>();
        Iterator<Map.Entry<String, String[]>> entries = properties.entrySet().iterator();

        Map.Entry<String, String[]> entry;
        String name = "";
        String value = "";
        Object valueObj =null;
        while (entries.hasNext()) {
            entry = entries.next();
            name = (String) entry.getKey();
            valueObj = entry.getValue();
            if (null == valueObj) {
                value = "";
            } else {
                String[] values = (String[]) valueObj;
                for (int i = 0; i < values.length; i++) {
                    value = values[i] + ",";
                }
                value = value.substring(0, value.length() - 1);
            }
            returnMap.put(name, value);
        }

        return payNoticeService.alipayNoticeHandle(returnMap);
    }

}
